<template>
    <div v-if="props.isShow" style="position:fixed;z-index: 10;width: 10000px;height: 100vh;top:110px;left:0;"></div>
    <el-form ref="form" :model="formData" readonly label-width="100px">
        <el-form-item label="学期" prop="" >
            <el-select v-model="formData.semesterId" placeholder="请选择学期" disabled style="width: 100%;text-align: center;">
                <el-option v-for="item in semesters" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
        </el-form-item>
        <el-form-item label="实验室" prop="">
            <el-select v-if="transmitObj==null" v-model="formData.laboratoryIds" multiple placeholder="请选择实验室" style="width: 100%;text-align: center;">
                <el-option v-for="item in laboratorys" :key="item.id" :label="item.name" :value="item.id" />
            </el-select>
            <el-select v-else v-model="formData.laboratoryId" placeholder="请选择实验室" style="width: 100%;text-align: center;">
                <el-option v-for="item in laboratorys" :key="item.id" :label="item.name" :value="item.id"/>
            </el-select>
        </el-form-item>
        <el-form-item label="门窗状态" prop="" width="100%">
            <div class="description">门窗是否损坏</div>
            <el-switch v-model="formData.doors_windows" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.doors_windows" v-model="formData.doors_windows_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="电器线路" prop="">
            <div class="description">表示电器线路是否正常</div>
            <el-switch v-model="formData.electrical_wiring" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.electrical_wiring" v-model="formData.electrical_wiring_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="消防器件" prop="">
            <div class="description">消防器件是否存在、能正常使用、是否过期</div>
            <el-switch v-model="formData.firefighting_facilities" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.firefighting_facilities" v-model="formData.firefighting_facilities_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="仪器设备" prop="">
            <div class="description">仪器设备是否丢失、可正常使用、存在安全隐患</div>
            <el-switch v-model="formData.instruments_equipment" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.instruments_equipment" v-model="formData.instruments_equipment_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="防盗设施" prop="">
            <div class="description">防盗设施是否完好无损</div>
            <el-switch v-model="formData.anti_theft_facilities" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.anti_theft_facilities" v-model="formData.anti_theft_facilities_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="警示标志" prop="">
            <div class="description">是否有警示标志</div>
            <el-switch v-model="formData.warning_sign" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.warning_sign" v-model="formData.warning_sign_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="清洁状态" prop="">
            <div class="description">表示物品的摆放和清洁情况是否良好</div>
            <el-switch v-model="formData.item_hygiene" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.item_hygiene" v-model="formData.item_hygiene_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <el-form-item label="其他问题" prop="">
            <div class="description">是否存在其他异常情况</div>
            <el-switch v-model="formData.other_safety_hazards" class="ml-2" inline-prompt
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949" active-text=" 正 常 "
                inactive-text=" 异 常 " />
            <el-input v-if="!formData.other_safety_hazards" v-model="formData.other_safety_hazards_state"
                placeholder="请输入具体情况" :disabled="props.isShow"></el-input>
        </el-form-item>
        <div v-if="!props.isShow" style="text-align:  center;margin-top: 20px;">
            <el-button style="margin-right: 50px;" @click="Close()">取消</el-button>
            <el-button type="primary" @click="SubmitForm()">确定</el-button>
        </div>
    </el-form>
</template>

<script setup>
import service from '@/request';
import { ElMessage } from 'element-plus';
import { defineProps, nextTick, onBeforeMount, onMounted, reactive ,ref} from 'vue'
// #region 显示提示
async function ShowTip(isSuccess, msg) {
    if (isSuccess == true) {
        ElMessage({ message: msg, type: 'success' })
    }
    else {
        ElMessage({ message: msg, type: 'error' })
    }
}
//#endregion

let props = defineProps(["Close", "transmitObj","isShow"])

let formData = reactive
    ({
        id: 0,
        dateTime: '',
        state: true,
        doors_windows: true,//门窗状态
        doors_windows_state: '',
        electrical_wiring: true,//电器线路状态
        electrical_wiring_state: '',
        firefighting_facilities: true,//消费器械是否能正常使用
        firefighting_facilities_state: '',
        firefighting_facilities_validity: true,//消费器械是否过期
        firefighting_facilities_validity_state: '',
        instruments_equipment: true,//仪器设备是否能正常使用
        instruments_equipment_state: '',
        anti_theft_facilities: true,//防盗设施是否完好
        anti_theft_facilities_state: '',
        warning_sign: true,//表示是否有警示标志
        warning_sign_state: '',
        iEOther_safety_hazards: true,//表示设备仪器是否存在其他安全隐患
        iEOther_safety_hazards_state: '',
        item_hygiene: true,//表示物品的摆放和清洁情况是否良好
        item_hygiene_state: '',
        other_safety_hazards: true,//其他安全隐患备注
        other_safety_hazards_state: '',

        semesterId: 0,//学期id
        laboratoryId: null,//实验室id
        laboratoryIds:[]
    })

//获取实验室
let laboratorys = reactive([])
async function GetLaboratorys() {
    let resultData = []
    let userId = JSON.parse(localStorage.getItem('userId'))
    await service.get("/Laboratory/getLaboratoryByUserId?userId=" + userId).then(
        (res) => {
            resultData.push(...res.value)
        }
    )
    return resultData
}

//获取当前学期
let currentSemester = reactive({})
async function GetCurrentSemester() {
    let resultData=null
    await service.get("/Mangment/getCurrentSemester").then(
        (res) => {
            resultData= res.value
        }
    )
    return resultData
}
//获取所有学期
let semesters=reactive([])
async function GetSemesters() {
    let resultData = []
    await service.get("/Mangment/getAllSemester").then(
        (res) => {
            resultData.push(...res.value)
        }
    )
    return resultData
}



async function SubmitForm() {
    if (formData.name == '') {
        ShowTip(false,'请先完成必要步骤')
        return
    }
    //添加
    if (props.transmitObj == null) {
        await service.post(`/Check/addCheck`,formData).then(
            async res => {
                ShowTip(true,"操作成功")
            },
            error => {
                ShowTip(false,error)
            }
        )
    }
    //编辑
    else {
        await service.post("/Check/updateCheck",formData).then(
            async res => {
                ShowTip(true,"操作成功")
            },
            error => {
                ShowTip(false,error)
            }
        )
    }
    await props.Close()
}


onBeforeMount(async () => {
    currentSemester= await GetCurrentSemester()
    formData.semesterId=currentSemester.id
    semesters.push(...await GetSemesters())
    laboratorys.push(... await GetLaboratorys())
    //如果是编辑操作就初始化
    if (props.transmitObj != null) {
        for (var p in props.transmitObj) {
            formData[p] = props.transmitObj[p]
        }
    }
})

</script>

<style scoped>
.description {
    width: 80%;
    font-size: 13px;
    color: darkgray;
}
</style>